<template>
    <div class="login-container">
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <h3 class="title">系统登录</h3>
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            v-model="loginForm.username"
            name="username"
            type="text"
            auto-complete="on"
            placeholder="用户名"
          />
        </el-form-item>
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            v-model="loginForm.password"
            :type="passwordType"
            name="password"
            auto-complete="on"
            placeholder="密码"
            @keyup.enter="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <el-form-item>
          <el-button
            :loading="loading"
            type="primary"
            style="width: 100%"
            @click.prevent="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </template>
  
  <script lang="ts" setup>
  import { ref, reactive } from 'vue'
  import { useRouter } from 'vue-router'
  import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
  import userStore from '../../store/modules/user'
  
  const router = useRouter()
  const store = userStore()
  
  const loginForm = reactive({
    username: 'zhangsan',
    password: '123456'
  })
  
  const loginRules = reactive<FormRules>({
    username: [
      { required: true, message: '请输入用户名', trigger: 'blur' },
      { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
    ]
  })
  
  const passwordType = ref('password')
  const loading = ref(false)
  const loginFormRef = ref<FormInstance>()
  
  const showPwd = () => {
    passwordType.value = passwordType.value === 'password' ? '' : 'password'
  }
  
  const handleLogin = async () => {
    if (!loginFormRef.value) return
    
    try {
      const valid = await loginFormRef.value.validate()
      if (!valid) return
      
      loading.value = true
      await store.userLogin(loginForm, () => {
        router.push({ path: '/menu' })
      })
    } catch (error:any) {
      console.error(error)
      ElMessage.error(error.message || '登录失败')
    } finally {
      loading.value = false
    }
  }
  </script>
  
  <style lang="less" scoped>
  /* 样式保持不变 */
  .login-container {
    position: fixed;
    min-height: 100%;
    width: 100%;
    background: url("../../assets/login/login-bakg.jpg") no-repeat center;
    background-size: 100% 100%;
    overflow: hidden;
  
    .login-form {
      position: relative;
      width: 520px;
      max-width: 100%;
      padding: 150px 35px 0;
      margin: 0 auto;
      margin-top: 11%;
      overflow: hidden;
      opacity: 0.5;
      background-color: #fff;
    }
  
    .title {
      font-size: 26px;
      color: #131212;
      margin: 0 auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  
    .svg-container {
      padding: 6px 5px 6px 15px;
      color: #889aa4;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }
  
    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: #889aa4;
      cursor: pointer;
      user-select: none;
    }
  }
  </style>